/**
 * 背景层
 */
define(['createjs'],function(createjs){
	var PencilLayer = function(layer,width,height){
		//常用标识
		this._layer = layer;
		this.pencil = new createjs.Shape();
		this._width = width;
		this._height = height;
		
		//私有对象
		this._painterColor = false;
		this._painterLineWidth = false;
		this._painterMode = false;
		
		this._layer.cache(0,0,this._width,this._height);
		this._layer.addChild(this.pencil);

		/**
		 * 设置画笔颜色
		 * @param {Object} data 颜色对象
		 */
		this.setPainterColor = function( data ){
            var decimal = Math.floor( Number(data.r) * 255 ) * 65536 + Math.floor(Number(data.g) * 255 ) * 256 + Math.floor(Number(data.b) * 255 );
            if(isNaN(decimal))decimal = 0;
            var num = decimal.toString(16);
            while (num.length < 6) num = "0" + num;
            this._painterColor = "#" + num;
        }
		
		/**
		 * 设置画笔粗细
		 * @param {Object} data 画笔粗细对象
		 */
		this.setPainterLineWidth = function(data){
			this._painterLineWidth = data;
		}
		
		/**
		 * 设置混合模式
		 * @param {Object} data 画笔模式对象
		 */
		this.setPainterMode = function(data){
			if(data == 16){
                this._painterMode = "erase";
            }else{
                this._painterMode = "pencil";
            }
		}
		
		/**
		 * 画画
		 * @param {Object} pencilObj 要画的对象
		 * @param {Function} cb 绘制后的回调
		 */
		this.draw = function(pencilObj,cb){
			var graphics = this.pencil.graphics;

			//1.得到context
			this._graphics_arr || ( this._graphics_arr = [] );
			var x;
			for( x in this._graphics_arr ){
				if( this._graphics_arr[x] == pencilObj.stroke_id ){
					graphics = this._graphics_arr[x];
				}
			}
			graphics.strokeId = pencilObj.stroke_id;
			this._graphics_arr.push(graphics);

			switch (this._painterMode){
				case "erase":
					graphics.setStrokeStyle(this.__computeLineWidthFromSpeed(this._painterLineWidth,pencilObj),"round","round");
					graphics.beginStroke("#FFFFFF");//颜色
					if( pencilObj.phase == 0 ){
						this._last_erase_x = pencilObj.x;
						this._last_erase_y = pencilObj.y;
						
					}else if( pencilObj.phase == 1 ){
						if(!this._last_erase_x || !this._last_erase_y){
							this._last_erase_x = pencilObj.x;
							this._last_erase_y = pencilObj.y;
						}
						graphics.moveTo(this._last_erase_x, this._last_erase_y); // 移动到坐标 50 50
						graphics.lineTo( pencilObj.x, pencilObj.y ); // 划出轨迹到 150 150
						graphics.endStroke();
						this._last_erase_x = pencilObj.x;
						this._last_erase_y = pencilObj.y;
						this._layer.updateCache("destination-out");
						graphics.clear();
					}else{
						graphics.moveTo( this._last_erase_x, this._last_erase_y ); // 移动到坐标
						graphics.lineTo( pencilObj.x, pencilObj.y ); // 划出轨迹到 150 150
						graphics.endStroke();
						this._last_erase_x = this._last_erase_y = null; 
						this._layer.updateCache("destination-out");
						graphics.clear();
					}
					break;
				case "pencil":
					graphics.setStrokeStyle(this.__computeLineWidthFromSpeed(this._painterLineWidth,pencilObj),"round","round");
					graphics.beginStroke(this._painterColor);//颜色
					if( pencilObj.phase == 0 ){
						this._last_render_x = pencilObj.x;
						this._last_render_y = pencilObj.y;
					}else if( pencilObj.phase == 1 ){
						if(!this._last_render_x || !this._last_render_y){
							this._last_render_x = pencilObj.x;
							this._last_render_y = pencilObj.y;
						}
						graphics.moveTo(this._last_render_x, this._last_render_y); // 移动到坐标 50 50
						graphics.lineTo( pencilObj.x, pencilObj.y );
						graphics.endStroke();
						this._last_render_x = pencilObj.x;
						this._last_render_y = pencilObj.y;
						this._layer.updateCache("source-over");
						graphics.clear();
					}else{
						graphics.moveTo( this._last_render_x, this._last_render_y ); // 移动到坐标
						graphics.lineTo( pencilObj.x, pencilObj.y );
						graphics.endStroke();
						this._last_render_x = this._last_render_y = null;
						this._layer.updateCache("source-over");
						graphics.clear();
					}
					break;
				default:
					console.log("未定义的工具！");
					console.log(obj);
					break;
			}

			if(pencilObj.phase == 0 || pencilObj.phase == 1){
				cb && cb(pencilObj);
				return;
			}
				

			//3.删除context
			var remaining = [];
			var x;
			for(x in this._graphics_arr ){
				var graphics = this._graphics_arr[x];
				if( graphics.strokeId != pencilObj.stroke_id )
					remaining.push(graphics);
			}

			if (remaining.length)
				this._graphics_arr = remaining;
			else
				this._graphics_arr = null;
			cb && cb(pencilObj);
		}
		
		/**
		 * 清除画笔
		 */
		this.clear = function(){
			var graphics = this.pencil.graphics;
			graphics.beginFill("#FFFFFF").drawRect(0,0,this._width,this._height);
			this._layer.updateCache("destination-out");
			graphics.clear();
		}
		
		/**
		 * 根据速度计算画笔宽度
		 * @param {int} base_width 基础宽度
		 * @param {Object} obj 要计算的对象
		 */
		this.__computeLineWidthFromSpeed = function(base_width, obj){
			 var width = base_width;
                var level = 1; //平滑度
                if( obj.phase != 0 ){
                    var l = Math.sqrt( ( obj.x - this._last_x ) * ( obj.x - this._last_x ) + ( obj.y - this._last_y ) * ( obj.y - this._last_y ) );
                    var v = ( obj.timestamp - this._last_t ) / l;//v的倒数
                    //this._base_v || (this._base_v = v);
                    this._base_v = 0.0016;
                    var p = v / this._base_v ;
                    for( var i = 0; i<level; p = Math.sqrt(p),i++ );

                    var tmp_w = p * base_width;

                    if(tmp_w==0){tmp_w=base_width;}//2015.6.5=====JackYan

                    (width > tmp_w) && ( width = tmp_w );
                    //console.info("width:", width ," x:", obj.x," y:",obj.y," l:",l," v:",v );
                }
                if( obj.phase == 2 ){
                    this._last_t = this._last_x = this._last_y = this._base_v = void 0;
                }else{
                    this._last_t = obj.timestamp;
                    this._last_x = obj.x;
                    this._last_y = obj.y;
                }
                return width;
		}
		
	};
	
	return PencilLayer;
});